<?php

namespace App\Http\Controllers;

use App\Http\Resources\ArticleListCollection;
use App\Http\Resources\ArticleShowResource;
use App\Model\Article;

class ArticleController extends Controller
{
    public function lists($type)
    {
        switch ($type) {
            case 'notice':
                $type = 0;
                break;
            case 'btc':
                $type = 1;
                break;
            case 'other':
                $type = 2;
                break;
            case 'coin':
                $type = 3;
                break;
            default:
                $type = 0;
                break;
        }

        $articles = Article::select(['id', 'title', 'type', 'preview', 'created_at'])
            ->where('type', $type)
            ->orderBy('created_at', 'desc')
            ->paginate();
        if (! $articles->total()) {
            return response()->json(['error' => '文章不存在'], 404);
        }

        return new ArticleListCollection($articles);
    }

    /**
     * 新闻详情
     *
     * @param Article $article
     * @return mixed
     */
    public function show(Article $article)
    {
        return new ArticleShowResource($article);
    }
}